package com.yfproject.config;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.springframework.core.annotation.Order;

//第一种创建filter的方法
//public class TestFilter implements Filter{
//
//	public void init(FilterConfig filterConfig) throws ServletException {
//		// TODO Auto-generated method stub
//		
//	}
//
//	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
//			throws IOException, ServletException {
//		System.out.println("TestFilter");
//		
//	}
//
//	public void destroy() {
//		// TODO Auto-generated method stub
//		
//	}
//
//}

//第二种创建filter的方法
@Order(1)
@WebFilter(filterName = "testFilter1", urlPatterns = "/*")
public class TestFilterFirst implements Filter {
  public void init(FilterConfig filterConfig) throws ServletException {
	  System.out.println("==>TestFilterFirst启动");
  }
  private int a = 0;
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
          throws IOException, ServletException {
      while(a < 2) {
    	  System.out.println("TestFilter" + a);
    	  a = a + 1;
      }
      System.out.println("doFilter");
      a = 0;
      filterChain.doFilter(servletRequest,servletResponse);
  }

  public void destroy() {
	  System.out.println("==>TestFilterFirst结束");
  }
}

